import Vue from 'vue';
import App from './App.vue';
import router from './routes';
import store from './store';
import {
  Loading
} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import BaiduMap from 'vue-baidu-map';
import Mint from 'mint-ui';
import 'mint-ui/lib/style.css';
import LocalStorage from '@/services/storage';
import {
  entryPath
} from '@/env';

Vue.use(Mint);
Vue.use(Loading);

router.beforeEach((to, from, next) => {
  console.log("tooooooooooooo")
  console.log(to)
  console.log(from)
  console.log(next)
  // type 1 是公众号支付 2是微信H5支付（默认）
  let {
    type
  } = to.query;
  if (type) {
    sessionStorage.type = type;
  } else if (!sessionStorage.type) {
    sessionStorage.type = "2";
  }
  let token = LocalStorage.getLocalStorage('loginStatus');
  if (token) {
    store.dispatch('login', token);
  }
  /* 如果本地 存在 token 则 不允许直接跳转到 登录页面*/
  if (to.path == (entryPath + '/login')) {
    if (token) {
      next({
        path: from.fullPath
      });
    } else {
      next();
    }
  } else {
    if (to.matched.length === 0) {
      from.name ?
        next({
          name: from.name
        }) :
        next(entryPath + '/home');
    } else {
      if (to.query.type) {
        next();
      } else {
        console.log(1)
        next({
          name: to.name,
          query: {
            type: sessionStorage.type
          }
        });
      }
      if (to.meta.requireAuth) {
        // 判断该路由是否需要登录权限
        if (token) {
          // 通过vuex state获取当前的token是否存在
          next();
        } else {
          console.log(to.fullPath);
          next({
            path: entryPath + '/login',
            query: {
              redirect: to.fullPath,
              type: sessionStorage.type
            } // 将跳转的路由path作为参数，登录成功后跳转到该路由
          });
        }
      } else {
        console.log(2)
        next();
      }
    }
  }
});
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);
});
Vue.config.productionTip = false

Vue.use(BaiduMap, {
  // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
  ak: '3YOCg7ZoHfhOLxtsTDpMVuPs'
});

Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app');
